using System;
using System.Collections.Generic;
using System.Text;
using DevExpress.XtraBars;
using System.Windows.Forms;
using Microsoft.Practices.CompositeUI;
using Microsoft.Practices.CompositeUI.EventBroker;
using NetRosApp.Infrastructure.Interface.Constants;
using NetRosApp.Infrastructure.Interface.Base.BaseViews;
using Microsoft.Practices.ObjectBuilder;
using NetRosApp.Infrastructure.Interface.Services;

namespace NetRosApp.Infrastructure.Interface.Base
{
    [Service(typeof(IDevMenuItems))]
    public class DevMenuItemsService : IDevMenuItems
    {
        System.Drawing.Font DefaultFont = new System.Drawing.Font("Segoe UI", 12); 
        WorkItem _workItem;
        [ServiceDependency]
        public WorkItem WorkItem
        {
            get { return _workItem; }
            set { _workItem = value; }
        }
        public void DodajMeniNazive(String key,String Naziv)
        {
            if (!_workItem.Services.Get<IActionCatalogService>().CanExecute(key)) return;
            if (_meni.ContainsKey(key)) return;
            _meni.Add(key, Naziv);

        }
        public bool PostojiLi(String key)
        {
            if (_meni.ContainsKey(key)) return true;
            return false;

        }
        public DevMenuItemsService(WorkItem work)
        {
            WorkItem = work;
        }
        Dictionary<String, String> _meni = new Dictionary<string, string>();
        public void DodajMenuItem(String Kljuc,String Naziv, Type TipViewa, String NazivParenta)
        {
            if (!_workItem.Services.Get<IActionCatalogService>().CanExecute(Kljuc)) return;
            if (!_workItem.Services.Get<IActionCatalogService>().CanExecute(NazivParenta)) return;
            ToolStripMenuItem menuitem = new ToolStripMenuItem(Naziv);
            menuitem.Tag = TipViewa;
            WorkItem.UIExtensionSites[NazivParenta].Add(menuitem);
           
            WorkItem.EventTopics["Prikazi"].AddPublication(menuitem, "Click", WorkItem, PublicationScope.WorkItem);
        }
        // [InjectionConstructor]
        //public DevMenuItemsService(Microsoft.Practices.CompositeUI.WorkItem work)
        //{
        //    _workItem = work;
        //}

    
        public void DodajDevRegistracije(String NazivParenta, String NazivParentNijeMainMenu,string Pozicija)
        {
            DodajDevRegistracije(NazivParenta,NazivParentNijeMainMenu,Pozicija,false);
        }

        Dictionary<String, WeakReference> _LoadedMenus = new Dictionary<string, WeakReference>();
        public void DodajDevRegistracije(String NazivParenta, String NazivParentNijeMainMenu, string Pozicija, bool Grupiraj)
        {
            if (!_workItem.Services.Get<IActionCatalogService>().CanExecute(NazivParenta)) return;

            if (NazivParentNijeMainMenu != null && NazivParentNijeMainMenu.Length !=0)
            {
             
            }
            else
            {
                NazivParentNijeMainMenu = UIExtensionSiteNames.MainMenuDev;
            }
         
            if (!WorkItem.UIExtensionSites.Contains(NazivParenta))
            {
                BarSubItem item = new BarSubItem();
                //item.Font.Size = FontSize;
                if (_meni.ContainsKey(NazivParenta))
                {
                    item.Caption = _meni[NazivParenta];
                }
                else
                {
                    item.Caption = NazivParenta;
                }
                item.OwnFont = DefaultFont;
                item.UseOwnFont = true;
                item.Description = Pozicija;
                WorkItem.UIExtensionSites[NazivParentNijeMainMenu].Add(item);
                WorkItem.UIExtensionSites.RegisterSite(NazivParenta, (BarSubItem)item);
                _LoadedMenus.Add(NazivParenta, new WeakReference(item));
                if (_LoadedMenus.ContainsKey(NazivParentNijeMainMenu) && _LoadedMenus[NazivParentNijeMainMenu].IsAlive && Grupiraj)
                {
                    foreach (LinkPersistInfo var in (_LoadedMenus[NazivParenta].Target as BarSubItem).LinksPersistInfo)
                    {
                        if (var.Item == item) { var.BeginGroup = true; }
                    }

                    //                (_LoadedMenus[NazivParenta].Target as BarSubItem).LinksPersistInfo.Add(new LinkPersistInfo(menuitem, Grupiraj));
                }
               //_loo.ToString();
            }
        }
        public void DodajDevMenuItem(String Kljuc,String Naziv, Type TipViewa, String NazivParenta)
        {
            if (!_workItem.Services.Get<IActionCatalogService>().CanExecute(Kljuc)) return;
            if (!_workItem.Services.Get<IActionCatalogService>().CanExecute(NazivParenta)) return;
            
            if (!WorkItem.UIExtensionSites.Contains(NazivParenta) /*&& !_LoadedMenus.ContainsKey(NazivParenta)*/)
            {
                BarSubItem item = new BarSubItem();
                //item.Font.Size = FontSize;
                if (_meni.ContainsKey(NazivParenta))
                {
                    item.Caption = _meni[NazivParenta];
                }else
                {
                item.Caption = NazivParenta;
                }
                item.OwnFont = DefaultFont;
                item.UseOwnFont = true;
                item.MergeType = BarMenuMerge.MergeItems;
                WorkItem.UIExtensionSites[UIExtensionSiteNames.MainMenuDev].Add(item);
                WorkItem.UIExtensionSites.RegisterSite(NazivParenta, (BarSubItem)item);
                if (!_LoadedMenus.ContainsKey(NazivParenta))
                _LoadedMenus.Add(NazivParenta, new WeakReference(item));
            }

            BarButtonItem menuitem = new BarButtonItem();
            menuitem.OwnFont = DefaultFont;
            menuitem.UseOwnFont = true;
            menuitem.Caption = Naziv;
            menuitem.Tag = new EventArgs<Type, WorkItem>(TipViewa, _workItem);
            //menuitem.Tag = TipViewa;
            //       menuitem.Font.Size = FontSize;
            WorkItem.UIExtensionSites[NazivParenta].Add(menuitem);
            menuitem.ItemClick += new ItemClickEventHandler(PrikaziDev);
            //               WorkItem.Commands["PrikaziDevc"].AddInvoker(menuitem, "ItemClick");
            //         WorkItem.EventTopics["PrikaziDev"].AddPublication(menuitem, "ItemClick", WorkItem, PublicationScope.WorkItem);
        }
        
        public void DodajDevMenuItem(String Kljuc,String Naziv, Type TipViewa, String NazivParenta,string Pozicija,bool Grupiraj)
        {
            if (!_workItem.Services.Get<IActionCatalogService>().CanExecute(Kljuc)) return;
            if (!_workItem.Services.Get<IActionCatalogService>().CanExecute(NazivParenta)) return;

            if (!WorkItem.UIExtensionSites.Contains(NazivParenta))
            {
                BarSubItem item = new BarSubItem();
                //item.Font.Size = FontSize;
                if (_meni.ContainsKey(NazivParenta))
                {
                    item.Caption = _meni[NazivParenta];
                }
                else
                {
                    item.Caption = NazivParenta;
                }
                item.OwnFont = DefaultFont;
                item.UseOwnFont = true;
                item.MergeType = BarMenuMerge.MergeItems;
               // item.Description = Convert.ToString(Pozicija);
                WorkItem.UIExtensionSites[UIExtensionSiteNames.MainMenuDev].Add(item);
                WorkItem.UIExtensionSites.RegisterSite(NazivParenta, (BarSubItem)item);
                _LoadedMenus.Add(NazivParenta, new WeakReference(item));
            }
            //if (_LoadedMenus["NazivParent"].IsAlive && Grupiraj)
            //{
            //    (_LoadedMenus["NazivParent"].Target as BarSubItem).LinksPersistInfo.Add(new LinkPersistInfo(MenuItem,Grupiraj));
            //}
            BarButtonItem menuitem = new BarButtonItem();
            menuitem.OwnFont = DefaultFont;
            menuitem.UseOwnFont = true;
            menuitem.Caption = Naziv;
           // menuitem.Tag = TipViewa;
            menuitem.Tag = new EventArgs<Type, WorkItem>(TipViewa, _workItem);
            menuitem.Description = Pozicija;
            //       menuitem.Font.Size = FontSize;
            WorkItem.UIExtensionSites[NazivParenta].Add(menuitem);
            menuitem.ItemClick += new ItemClickEventHandler(PrikaziDev);

            if (_LoadedMenus.ContainsKey(NazivParenta) && _LoadedMenus[NazivParenta].IsAlive && Grupiraj)
            {
                foreach (LinkPersistInfo var in (_LoadedMenus[NazivParenta].Target as BarSubItem).LinksPersistInfo)
                {
                    if (var.Item == menuitem) { var.BeginGroup = true; }
                }
               
//                (_LoadedMenus[NazivParenta].Target as BarSubItem).LinksPersistInfo.Add(new LinkPersistInfo(menuitem, Grupiraj));
            }
            //               WorkItem.Commands["PrikaziDevc"].AddInvoker(menuitem, "ItemClick");
            //         WorkItem.EventTopics["PrikaziDev"].AddPublication(menuitem, "ItemClick", WorkItem, PublicationScope.WorkItem);
        }
        public void DodajDevMenuItemParametri(String Kljuc,String Naziv, Type TipViewa, String NazivParenta, int IDDok)
        {
            if (!_workItem.Services.Get<IActionCatalogService>().CanExecute(Kljuc)) return;
            if (!_workItem.Services.Get<IActionCatalogService>().CanExecute(NazivParenta)) return;
            if (!WorkItem.UIExtensionSites.Contains(NazivParenta))
            {
                BarSubItem item = new BarSubItem();
                //item.Font.Size = FontSize;
                if (_meni.ContainsKey(NazivParenta))
                {
                    item.Caption = _meni[NazivParenta];
                }
                else
                {
                    item.Caption = NazivParenta;
                }
                item.OwnFont = DefaultFont;
                item.UseOwnFont = true;
                WorkItem.UIExtensionSites[UIExtensionSiteNames.MainMenuDev].Add(item);
                WorkItem.UIExtensionSites.RegisterSite(NazivParenta, (BarSubItem)item);
            }

            BarButtonItem menuitem = new BarButtonItem();
            menuitem.OwnFont = DefaultFont;
            menuitem.UseOwnFont = true;
            menuitem.Caption = Naziv;
            menuitem.Tag = new EventArgs<Type,WorkItem, int>(TipViewa,_workItem, IDDok);
            //       menuitem.Font.Size = FontSize;
            WorkItem.UIExtensionSites[NazivParenta].Add(menuitem);
            menuitem.ItemClick += new ItemClickEventHandler(PrikaziDevParametri);
            //               WorkItem.Commands["PrikaziDevc"].AddInvoker(menuitem, "ItemClick");
            //         WorkItem.EventTopics["PrikaziDev"].AddPublication(menuitem, "ItemClick", WorkItem, PublicationScope.WorkItem);
        }

        //public class ItemClickNewEventArgs : ItemClickEventArgs
        //{
        //    public ItemClickEventArgs
        //}

        //[EventSubscription("Prikazi", Thread = ThreadOption.UserInterface)]
        //public void Prikazi(object sender, EventArgs e)
        //{
        //    ToolStripMenuItem tmpmenuitem = sender as ToolStripMenuItem;
        //    WorkItem.Workspaces[Constants.WorkspaceNames.RightWorkspace].Show(WorkItem.SmartParts.Get((tmpmenuitem.Tag as Type).ToString()) ?? WorkItem.SmartParts.AddNew(tmpmenuitem.Tag as Type, (tmpmenuitem.Tag as Type).ToString()));
        //}

        //  [EventSubscription("PrikaziDev")]
        public void PrikaziDev(object sender, ItemClickEventArgs e)
        {
            WorkItem _workItem2 = (e.Item.Tag as EventArgs<Type, WorkItem>).Data1;
            Cursor.Current = Cursors.WaitCursor;
            WorkItem.EventTopics.Get(EventTopicNames.StatusUpdate).Fire(sender, new EventArgs<string>("Loading...."), _workItem2, PublicationScope.Global);
            BarButtonItem tmpmenuitem = e.Item as BarButtonItem;

            //WorkItem.Workspaces[Constants.WorkspaceNames.RightWorkspace].Show(WorkItem.SmartParts.Get((tmpmenuitem.Tag as Type).ToString()) ?? WorkItem.SmartParts.AddNew(tmpmenuitem.Tag as Type, (tmpmenuitem.Tag as Type).ToString()));

            //            BaseEditWorkItem tmpitm = WorkItem.WorkItems.Get((tmpmenuitem.Tag as Type).ToString()) as BaseEditWorkItem;

            //if (tmpitm == null)
            //{
            //     tmpitm = new BaseEditWorkItem((tmpmenuitem.Tag as Type));
            BaseEditWorkItem tmpitm = new BaseEditWorkItem((tmpmenuitem.Tag as EventArgs<Type,WorkItem>).Data);
            _workItem2.WorkItems.Add(tmpitm);
            //                 WorkItem.WorkItems.Add(tmpitm, (tmpmenuitem.Tag as Type).ToString());
            //          }

            tmpitm.Start(null, null);
            Cursor.Current = Cursors.Default;
            _workItem2.EventTopics.Get(EventTopicNames.StatusUpdate).Fire(this, new EventArgs<string>("Ready"), _workItem2, PublicationScope.Global);
        }

        public void PrikaziDevParametri(object sender, ItemClickEventArgs e)
        {
            WorkItem _workItem2 = (e.Item.Tag as EventArgs<Type, WorkItem,int>).Data1;
            Cursor.Current = Cursors.WaitCursor;
            WorkItem.EventTopics.Get(EventTopicNames.StatusUpdate).Fire(sender, new EventArgs<string>("Loading...."), _workItem2, PublicationScope.Global);
            BarButtonItem tmpmenuitem = e.Item as BarButtonItem;

            //WorkItem.Workspaces[Constants.WorkspaceNames.RightWorkspace].Show(WorkItem.SmartParts.Get((tmpmenuitem.Tag as Type).ToString()) ?? WorkItem.SmartParts.AddNew(tmpmenuitem.Tag as Type, (tmpmenuitem.Tag as Type).ToString()));

            //            BaseEditWorkItem tmpitm = WorkItem.WorkItems.Get((tmpmenuitem.Tag as Type).ToString()) as BaseEditWorkItem;

            //if (tmpitm == null)
            //{
            //     tmpitm = new BaseEditWorkItem((tmpmenuitem.Tag as Type));
            BaseEditWorkItem tmpitm = new BaseEditWorkItem((tmpmenuitem.Tag as EventArgs<Type, WorkItem, int>).Data);
            _workItem2.WorkItems.Add(tmpitm);
            //                 WorkItem.WorkItems.Add(tmpitm, (tmpmenuitem.Tag as Type).ToString());
            //          }
            // EventArgs<object,object> tmp = new EventArgs<object,object>(null,new EventArgs<int>((tmpmenuitem.Tag as EventArgs<Type,int>).Data1));
            tmpitm.Start(null, new EventArgs<int>((tmpmenuitem.Tag as EventArgs<Type, WorkItem, int>).Data2));
            Cursor.Current = Cursors.Default;
            WorkItem.EventTopics.Get(EventTopicNames.StatusUpdate).Fire(this, new EventArgs<string>("Ready"), _workItem2, PublicationScope.Global);
        }
    }

}
